package com.huangsm.juc.threadpool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class TestScheduledThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool =
                Executors.newScheduledThreadPool(5);
        List< ScheduledFuture<Double>>list=new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            ScheduledFuture<Double> schedule = pool.schedule(new Callable<Double>() {
                @Override
                public Double call() throws Exception {
                    double random = Math.random();
                    System.out.println(Thread.currentThread().getName() + random);
                    return random;
                }
            }, 3, TimeUnit.SECONDS);
            list.add(schedule);
        }
        pool.shutdown();
        list.forEach(
                s->{
                    try {
                        System.out.println(s.get());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
        );
    }
}
